package com.chinaunicom.behavior.memento;

import java.util.Stack;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2022/8/18 14:09
 * Description:
 */
public class Client {
    public static void main(String[] args) {
        // 定义栈结构，存储 Caretaker 数据
        Stack<Caretaker> stack = new Stack<>();
        
        Caretaker caretaker = new Caretaker();
        Originator originator = new Originator();
        originator.setState("test");

        
        // 在对数据备份时，将当前数据保存到 Caretaker 栈中
        Memento memento = originator.undo();
        caretaker.setMemento(memento);
        stack.push(caretaker);
        
        System.out.println("memento info : " + memento.toString());
        
        originator.setState("undo");
        System.out.println("memento info : " + originator.toString());
        
        
        // 恢复时，从Caretaker栈中弹出顶部 Caretaker对象
        Caretaker pop = stack.pop();
        originator.restore(pop.getMemento());
        System.out.println("memento info : " + originator.getState());

        
        // 当前栈中存在数据对象个数
        System.out.println("stack object size: " + stack.size());
    }
}
